<template>
	<u-modal :show="show" :negativeTop="200" :title="title" confirmText="确定" showCancelButton confirmColor="#FD8906"
		cancelColor="#000" @confirm="save" @cancel="show = false" @close="show = false" zoom>
		<view class="f-1 w100 f14 flex box pt10">
			<view class="f-g-0 table f-c f12" v-for="(v,i) in tabelList" :key="i" :class="{'ac':curr == i}"
				@click="sTable(v,i)">{{v.type.name}}{{v.name}}</view>
		</view>
	</u-modal>
</template>

<script>
	export default {
		components: {

		},
		props: {
			mode: {
				type: String,
				default: () => 'single'
			},
			v: {
				type: Object,
				default: () => {}
			}
		},
		data() {
			return {
				subColor: uni.getStorageSync('subject_color'),
				form: {},
				show: false,
				title: '转台',
				t: 'turntable',
				tabelList: [],
				curr: -1,
				toTable: 0,
				state: '',
			}
		},
		methods: {
			async getTableList() {
				const {
					data: {
						list
					}
				} = await this.beg.request({
					url: this.api.inTabel,
					data: {
						areaId: '',
						state: this.state,
						pageSize: 999,
					}
				})
				this.tabelList = list ? list : []
				if (this.tabelList && this.tabelList.length) {
					const i = this.tabelList.findIndex(v => v.id == this.v.id)
					if(i>=0){
						this.tabelList.splice(i, 1)
					}
				}
			},
			open(t) {
				this.t = t
				if (t == 'turntable') {
					this.title = `将${this.v.type.name}${this.v.name}转到哪里`
					this.state = 'free'
				} else if (t == 'parallel') {
					this.title = `将${this.v.type.name}${this.v.name}并到哪里`
					this.state = 'settle'
				}
				this.getTableList()
				this.show = true
			},
			close() {
				this.show = false
			},
			sTable(v, i) {
				this.curr = i
				this.toTable = v.id
			},
			async save() {
				if (this.curr < 0) {
					return uni.showToast({
						title: '请选择桌台',
						icon: 'none'
					})
				}
				let {
					code,
					msg
				} = await this.beg.request({
					url: `${this.t=='parallel'?this.api.combineTable:this.api.changeTable}/${this.v.id}`,
					method: 'POST',
					data: {
						storeId: this.v.storeId,
						toTable: this.toTable
					}
				})
				uni.showToast({
					title: msg,
					icon: 'none'
				})
				if (code && code == 200) {
					this.toTable = ''
					this.show = false
					setTimeout(() => {
						// #ifdef MP-WEIXIN
							uni.reLaunch({
								url: `/pages/order/orders/index`
							})
						// #endif
						// #ifndef MP-WEIXIN
							uni.switchTab ({
								url: `/pages/index/index`
							})
						// #endif
					}, 500)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.f40 {
		font-size: 40rpx;
	}

	.f42 {
		font-size: 42rpx;
	}

	.box {
		height: 600rpx;
		flex-wrap: wrap;
		overflow-y: scroll;
	}

	.table {
		width: 165rpx;
		height: 90rpx;
		border: 2rpx solid #e5e5e5;
		margin-right: 20rpx;
		margin-bottom: 20rpx;
	}

	.ac {
		font-weight: bold;
		background: #FDDA34;
	}
</style>